#include common_scripts\utility;
#include maps\mp\_utility;
#include maps\mp\gametypes\_hud_util;
#include z_mod\utility;


init()
{
    level thread onPlayerConnected();
}

onPlayerConnected()
{
    for(;;) {
        level waittill("connected", player);
        
        //player thread onPlayerSpawned();
        player thread classMenu_Create();
        player thread weaponMenu_Create();
    }
}

onPlayerSpawned()
{
    self endon("disconnect");
    
    for(;;) {
        self waittill("spawned_player");
    }
}

classMenu_Create()
{
    self.zMenu_ClassSelect = [];
    
    self.zMenu_ClassSelect[0] = self createFontString("hudbig", 1);
    self.zMenu_ClassSelect[0].alpha = 0;
    self.zMenu_ClassSelect[0].foreGround = true;
    self.zMenu_ClassSelect[0].sort = 1;
    self.zMenu_ClassSelect[0] setPoint("CENTER LEFT", "CENTER LEFT", 5, -100);
    self.zMenu_ClassSelect[0] setText("Class ^1s^7elect menu:");
    
    for(i = 0; i < level.zClass_List.size; i++) {
        self.zMenu_ClassSelect[i+1] = self createFontString("objective", 1);
        self.zMenu_ClassSelect[i+1].alpha = 0;
        self.zMenu_ClassSelect[i+1].color = (0.8, 0.8, 0); // yellow
        self.zMenu_ClassSelect[i+1].foreGround = true;
        self.zMenu_ClassSelect[i+1].sort = 2;
        self.zMenu_ClassSelect[i+1] setParent(self.zMenu_ClassSelect[0]);
        self.zMenu_ClassSelect[i+1] setPoint("BOTTOM RIGHT", "BOTTOM RIGHT", 100, 20 + i*10);
        self.zMenu_ClassSelect[i+1] setText(level.zClass_List[i].name);
        
        wait(0.01);
    }
    
    self.zMenu_ClassSelect[level.zClass_List.size+1] = self createIcon("black", 240, 85);
    self.zMenu_ClassSelect[level.zClass_List.size+1].alpha = 0;
    self.zMenu_ClassSelect[level.zClass_List.size+1].foreGround = true;
    self.zMenu_ClassSelect[level.zClass_List.size+1].sort = 0;
    self.zMenu_ClassSelect[level.zClass_List.size+1] setParent(self.zMenu_ClassSelect[0]);
    self.zMenu_ClassSelect[level.zClass_List.size+1] setPoint("BOTTOM RIGHT", "BOTTOM RIGHT", 230, 70);
    
    self thread classMenu_Logic();
}

classMenu_Hide()
{
    for(i = 0; i < self.zMenu_ClassSelect.size; i++)
        self.zMenu_ClassSelect[i].alpha = 0;
}

classMenu_Show()
{
    for(i = 0; i < self.zMenu_ClassSelect.size; i++)
        self.zMenu_ClassSelect[i].alpha = 1;
}

classMenu_Destroy()
{
    if(isDefined(self.zMenu_ClassSelect) && isDefined(self.zMenu_ClassSelect[0])) {
        for(i = 0; i < self.zMenu_ClassSelect.size; i++)
            self.zMenu_ClassSelect[i] delete();
    }
}

classMenu_Logic()
{
    self endon("disconnect");
    
    for(;;) {
        result = self waittill_any_return("zkey_actionslot_2", "zkey_activate", "death", "disconnect");
        
        if(result == "zkey_actionslot_2" && !self.zMenu_UsingClassMenu && !self.zMenu_UsingWeaponMenu && self.zClass_CanChange && self isOnGround()) { // opening the menu
            self.zMenu_UsingClassMenu = true;
            self freezeControlsWrapper(true);
            self classMenu_Show();
            
            self thread classMenu_Selecting();
        } else if(result == "zkey_actionslot_2" && self.zMenu_UsingClassMenu) { // closing the menu
            self notify("zmenu_close");
            
            self.zMenu_UsingClassMenu = false;
            self freezeControlsWrapper(false);
            self classMenu_Hide();
        } else if(result == "zkey_activate" && self.zMenu_UsingClassMenu) { // trying to take some class
            if(!isDefined(level.zClass_List[self.zClass_Wanted].busy)) { // if class is not busy by someone
                self notify("zmenu_close");
                
                self.zMenu_UsingClassMenu = false;
                self freezeControlsWrapper(false);
                self classMenu_Hide();
                
                self z_mod\_class::changeClass(self.zClass_Wanted);
            } else {
                self iPrintLnBold("Class " + level.zClass_List[self.zClass_Wanted].name + " is busy by ^1" + level.zClass_List[self.zClass_Wanted].busy.name);
            }
        } else if(result == "death") { // dying - this should not be
            self notify("zmenu_close");
            
            self.zMenu_UsingClassMenu = false;
            self classMenu_Hide();
        } else { // disconnecting (probably it isnt even used)
            setDvar("TRALL_DVAR_KAROCH", "BATRUDINAV)))0");
            self classMenu_Destroy();
        }
    }
}

classMenu_Selecting()
{
    self endon("disconnect");
    self endon("zmenu_close");
    
    self.zMenu_ClassSelect[self.zClass_Wanted+1].color = (1, 0, 0);
    
    for(;;) {
        result = self waittill_any_return("zkey_forward", "zkey_back");
        
        if(result == "zkey_forward") {
            if(self.zClass_Wanted > 0) {
                self.zMenu_ClassSelect[self.zClass_Wanted+1].color = (0.8, 0.8, 0);
                self.zClass_Wanted--;
                self.zMenu_ClassSelect[self.zClass_Wanted+1].color = (1, 0, 0);
            }
        } else if(result == "zkey_back") {
            if(self.zClass_Wanted < (level.zClass_List.size-1)) {
                self.zMenu_ClassSelect[self.zClass_Wanted+1].color = (0.8, 0.8, 0);
                self.zClass_Wanted++;
                self.zMenu_ClassSelect[self.zClass_Wanted+1].color = (1, 0, 0);
            }
        }
        
        wait(0.01);
    }
}

weaponMenu_Create()
{
    self.zMenu_WeaponSelect = [];
    
    self.zMenu_WeaponSelect[0] = self createIcon("black", 75, 38); // weapon
    self.zMenu_WeaponSelect[0].alpha = 0;
    self.zMenu_WeaponSelect[0].foreGround = true;
    self.zMenu_WeaponSelect[0].hideWhenInMenu = true;
    self.zMenu_WeaponSelect[0] setPoint("CENTER", "CENTER", 90, -110);
    
    self.zMenu_WeaponSelect[1] = self createIcon("black", 32, 32); // attach 1
    self.zMenu_WeaponSelect[1].alpha = 0;
    self.zMenu_WeaponSelect[1].foreGround = true;
    self.zMenu_WeaponSelect[1].hideWhenInMenu = true;
    self.zMenu_WeaponSelect[1] setPoint("CENTER", "CENTER", 140, -110);
    
    self.zMenu_WeaponSelect[2] = self createIcon("black", 32, 32); // attach 2
    self.zMenu_WeaponSelect[2].alpha = 0;
    self.zMenu_WeaponSelect[2].foreGround = true;
    self.zMenu_WeaponSelect[2].hideWhenInMenu = true;
    self.zMenu_WeaponSelect[2] setPoint("CENTER", "CENTER", 190, -110);
}

weaponMenu_Visible(weapon, attach1, attach2)
{
    self.zMenu_WeaponSelect[0].alpha = weapon;
    self.zMenu_WeaponSelect[1].alpha = attach1;
    self.zMenu_WeaponSelect[2].alpha = attach2;
}

